package com.project.service;

import com.alibaba.fastjson.JSONObject;
import com.project.dao.AreaMapper;
import com.project.entity.Area;
import com.project.service.base.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//让checked例外也回滚
@Transactional(rollbackFor=Exception.class)
@Service
public class AreaServiceImpl extends BaseService implements AreaService{

    private static AreaMapper areaMapper = null;
    public void setMapper()
    {
        if(areaMapper==null)
            areaMapper = sqlSessionTemplate.getMapper(AreaMapper.class);//1.获取相关mapper
    }


    /**
     * author: WheelChen
     * date: 2017/9/20
     * function: 添加单条
     *
     * @param area
     * @return
     */
    @Override
    public int addOne(Area area) {
        setMapper();
        return areaMapper.insertSelective(area);
    }

    /**
     * author: WheelChen
     * date: 2017/9/20
     * function: 删除单条
     *
     * @param id
     * @return
     */
    @Override
    public int removeOneById(Integer id) {
        setMapper();
        return areaMapper.deleteByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017/9/20
     * function: 修改单条记录
     *
     * @param area
     * @return
     */
    @Override
    public int modifyOneById(Area area) {
        setMapper();
        return areaMapper.updateByPrimaryKeySelective(area);
    }

    /**
     * author: WheelChen
     * date: 2017/9/20
     * function: 查找单条
     *
     * @param id
     * @return
     */
    @Override
    public Area findOneById(Integer id) {
        setMapper();
        return areaMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017/9/20
     * function: 通过id查找省/市/区
     *
     * @param id 区id
     * @return
     */
    @Override
    public JSONObject findOneDetailById(Integer id) {
        setMapper();
        return areaMapper.selectOneDetailById(id);
    }
}
